import { get } from './base'

export function processSongs(songs) { // 获取歌曲的url
  if (!songs.length) {
    return Promise.resolve(songs)
  }
  return get('/api/getSongsUrl', {
    mid: songs.map((song) => {
      return song.mid
    })
  }).then((result) => {
    const map = result.map
    return songs.map((song) => {
      song.url = map[song.mid]
      return song
    }).filter((song) => {
      return song.url.indexOf('vkey') > -1
    })
  })
}
const lyrcMap = {} // 存储歌曲的mid和歌词
export function getLyric(song) { // 获取歌词请求 切换歌曲的时候调用
  if (song.lyric) { // 如果歌曲已经有歌词 直接返回
    return Promise.resolve(song.lyric)
  }
  const mid = song.mid
  const lyric = lyrcMap[mid]
  if (lyric) { // 如果可以拿到歌曲的id直接返回存储的歌词，不用发送请求
    return Promise.resolve(lyric)
  }
  console.log('dsfsfsf')
  return get('/api/getLyric', {
    mid
  }).then((result) => {
    const lyric = result ? result.lyric : '[00:00:00]该歌曲暂时无法获取歌词'
    lyrcMap[mid] = lyric // 存储歌曲id 和歌词
    return lyric
  })
}
